package com.example.mydemo103;

import androidx.appcompat.app.AppCompatActivity;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;

import com.example.mydemo103.Adapter.MyPagerAdapter;
import com.example.mydemo103.AdvFragment.FragmentOne103;
import com.example.mydemo103.AdvFragment.FragmentThree103;
import com.example.mydemo103.AdvFragment.FragmentTwo103;

import java.util.ArrayList;
import java.util.List;

public class AdvertActivity extends AppCompatActivity {
    private MyPagerAdapter myPagerAdapter;
    private ViewPager viewPager;
    List<Fragment> fragmentList;
    private Button btn;
    int[] rbsid={R.id.radioButton1,R.id.radioButton2,R.id.radioButton3};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_advert);
        btn=findViewById(R.id.button);
//        广告栏三个页面
        viewPager=findViewById(R.id.viewpageOne);
        fragmentList=new ArrayList<>();
        fragmentList.add(new FragmentOne103());
        fragmentList.add(new FragmentTwo103());
        fragmentList.add(new FragmentThree103());
        myPagerAdapter=new MyPagerAdapter(getSupportFragmentManager(),fragmentList);
        viewPager.setAdapter(myPagerAdapter);

//        去除标题栏
        if (getSupportActionBar() != null){
            getSupportActionBar().hide();
        }

        final RadioButton[] rbs=new RadioButton[3];
        for(int i=0;i<rbs.length;i++){
            rbs[i]=findViewById(rbsid[i]);
            final int finalI = i;
            rbs[i].setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    viewPager.setCurrentItem(finalI);
                }
            });
        }
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                rbs[position].setChecked(true);
            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    protected void onPause() {
        super.onPause();
        finish();
    }
}